package com.mingtu.guards.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.StringUtils;
import com.mingtu.center.bean.PatrolPeopleBean;
import com.mingtu.common.utils.MyUtills;
import com.mingtu.guards.R;

import java.util.LinkedList;

public class RecordTrackAdapter extends RecyclerView.Adapter<RecordTrackAdapter.ActivityHolder> implements View.OnClickListener {
    private Context context;
    private LinkedList<PatrolPeopleBean.PageBean.ListBean> mData;
    private OnItemClickListener mOnItemClickListener = null;


    public RecordTrackAdapter(LinkedList<PatrolPeopleBean.PageBean.ListBean> data) {
        this.mData = data;
    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_track_record, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view);
        viewHolder.itemView.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {
        if (mData != null) {
            PatrolPeopleBean.PageBean.ListBean listBean = mData.get(position);
            String name = listBean.getName();
            String totalMileage = listBean.getTotalMileage();
            String times = listBean.getNum();
            String duration = listBean.getTotalDuration();

            if (!StringUtils.isEmpty(duration)) {
                long d = 0;
                if (duration.indexOf(".") != -1) {
                    String array[] = duration.split("\\.");
                    d = Long.parseLong(array[0]);
                } else {
                    d = Long.parseLong(duration);
                }
                holder.tvTime.setText(MyUtills.formatSeconds5(d));
            }

            if (!StringUtils.isEmpty(totalMileage)) {
                holder.tvMileage.setText(totalMileage + "km");
            }

            if (!StringUtils.isEmpty(name)) {
                holder.tvName.setText(name);
            }

            if (!StringUtils.isEmpty(times)) {
                holder.tvTimes.setText(times);
            }

        }
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public void upData(LinkedList<PatrolPeopleBean.PageBean.ListBean> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void clearData() {
        mData.clear();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static class ActivityHolder extends RecyclerView.ViewHolder {
        private final View convertView;
        private final TextView tvName;
        private final TextView tvMileage;
        private final TextView tvTime;
        private final TextView tvTimes;
        private final TextView tvCheck;

        public ActivityHolder(View itemView) {
            super(itemView);
            convertView = itemView;
            tvName = (TextView) convertView.findViewById(com.mingtu.center.R.id.tv_name);
            tvMileage = (TextView) convertView.findViewById(com.mingtu.center.R.id.tv_mileage);
            tvTime = (TextView) convertView.findViewById(com.mingtu.center.R.id.tv_time);
            tvTimes = (TextView) convertView.findViewById(com.mingtu.center.R.id.tv_times);
            tvCheck = (TextView) convertView.findViewById(com.mingtu.center.R.id.tv_check);
        }
    }


}
